#ifndef _BLOCKSMATRIX_H_
#define _BLOCKSMATRIX_H_

#include "Image.h"
#include "Constants.h"
#include "TetradFactory.h"
#include <vector>

class CTetrad;

class CMatrixEventsListener
{
public:
	CMatrixEventsListener()  { }
	virtual ~CMatrixEventsListener()  { }
	virtual void OnStartRemoveLines() = 0;
	virtual void OnLinesRemoved(int iLinesCount) = 0;
	virtual void OnMatrixFull() = 0;
};

class CBlocksMatrix
{
public:
	CBlocksMatrix(CMatrixEventsListener* pListener, int xPos, int yPos);
	~CBlocksMatrix();
	void Draw();
	void Update(DWORD dwCurrentTime);
	void Reset();
	void ShapeLeft();
	void ShapeRight();
	void ShapeDown();
	void ShapeRotate();
	void move_ngan();
	void taongan();
	void move_ga();
	void taoga();
	void move_vit();
	void taotrungga();
	void taotrungvit();
	void taotrungngan();
	void taovit();
	bool IsCellFree(int XPos, int YPos);
	void FillCell(int XPos, int YPos, EBlockColor BlockColor);
	void GetScreenPosFromCell(int cellXPos, int cellYPos, 
		int& screenXPos, int& screenYPos);
	void SetTetradUpdate(int iNewUpdate)  { m_iTetradUpdate = iNewUpdate; }
	int  GetTetradUpdate() const  { return m_iTetradUpdate; }
	int tocdo;
	int capdo;
	int sotrunghunh;
	int sotrungout;
	int bathung;
	int tangtoc;
private:
	bool CheckMatrix();
	bool IsLineRemoved(int iRow);
	void RemoveLines();
	void NewShape();
	int m_iXPos;
	int m_iYPos;
	int matran[15];
	int m_pBlocksMatrix[13][13];
	TImagePtr conngan[1];
	TImagePtr conga[1];
	TImagePtr convit[1];
	TImagePtr cairo[1];
	TImagePtr trung_gam[2];
	TImagePtr trung_vitm[2];
	TImagePtr trung_nganm[2];
	CTetradFactory m_TetradFactory;
	CTetrad* trung_ga1;
	CTetrad* trung_ga1doi;
	CTetrad* trung_ga2;
	CTetrad* trung_ga2doi;
	CTetrad* trung_vit1;
	CTetrad* trung_vit1doi;
	CTetrad* trung_vit2;
	CTetrad* trung_vit2doi;
	CTetrad* trung_ngan1;
	CTetrad* trung_ngan1doi;
	CTetrad* trung_ngan2;
	CTetrad* trung_ngan2doi;
	CTetrad* ngan;
	CTetrad* tro;
	CTetrad* vit;
	CTetrad* trovit;
	CTetrad* ga;
	CTetrad* troga;
	CTetrad* ro;
	int kh;
	int ko;
	int kp;
	DWORD m_dwLastShapeDown;
	bool m_bRemovingLine;
	 //kiem tra va hien con ngan
	bool n1;  
	bool n2; 
	//kiem tra va hien vit
	bool v1;  
	bool v2; 
	bool checkv;
	//kiem tra va hien ga
	bool g1;  
	bool g2; 
	bool checkg;
	int setngan;
	int setvit;
	int setga;
	int setngan1;
	int setvit1;
	int setga1;
	  //kiem tra su duy chuyen cua con ngang
	bool kn;          
	int	 m_iLineBlinkCount;
	bool m_bLineBlinkOn;
	std::vector<int> m_vecLinesRemoved;

	CMatrixEventsListener* m_pListener;

	int m_iTetradUpdate;

	bool m_bGameOver;
};

#endif  